package nl.Viewer.Dialogs.DatasetSelection;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import nl.Viewer.Application;
import nl.Viewer.SerializedObjects.Dataset;
import nl.Viewer.SerializedObjects.Modality;
import nl.Viewer.SerializedObjects.Visualization;

/**
 * 
 * The panel responsible for creating a tree with the modalities and visualizations for a selected dataset
 * 
 * @author Jeroen Verschuur
 * 
 */

public class ModalitySelectionPanel extends JPanel implements MouseListener, KeyListener {
   private static final long serialVersionUID = -1;
   private Dataset           dataset          = null;
   private JTree             datasetTree;

   public ModalitySelectionPanel( Dataset dataset ) {
      super( new BorderLayout() );
      this.dataset = dataset;

      initializeComponent();

   }

   private void initializeComponent() {
      // ImageIcon icon1 = new ImageIcon("orbita.jpg");
      // ImageIcon icon2 = new ImageIcon("mri_brain.jpg");
      final DefaultMutableTreeNode root = new DefaultMutableTreeNode( dataset.name );

      for( Modality m : dataset.getModalities().values() ) {

         if( m.getVisualizations().size() > 0 ) {

            ModalityItemPanel itemPanel = new ModalityItemPanel( m );
            DefaultMutableTreeNode modalityNode = new DefaultMutableTreeNode( itemPanel );

            for( Visualization v : m.getVisualizations().values() ) {
               VisualizationItemPanel subItemPanel = new VisualizationItemPanel( v );
               DefaultMutableTreeNode visualizationNode = new DefaultMutableTreeNode( subItemPanel );
               modalityNode.add( visualizationNode );
            }
            root.add( modalityNode );
         }
      }

      datasetTree = new JTree( root );
      datasetTree.setRowHeight( 0 );
      datasetTree.setTransferHandler( new DatasetTreeTransferHandler() );
      datasetTree.setDragEnabled( true );
      datasetTree.addMouseListener( this );
      datasetTree.addKeyListener( this );

      add( new JScrollPane( datasetTree ), BorderLayout.CENTER );

   }

   public void mousePressed( MouseEvent e ) {
   }

   public void mouseReleased( MouseEvent e ) {
   }

   public void mouseExited( MouseEvent e ) {
   }

   public void mouseEntered( MouseEvent e ) {
   }

   public void handleActionOnSelectedTreePaths( TreePath[] treePaths, boolean collapseOnModality ) {
      if( treePaths == null )
         return;
      
      for( TreePath tp : treePaths ) {
         if( tp != null ) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tp.getLastPathComponent();
            if( selectedNode.getUserObject() instanceof VisualizationItemPanel ) {
               // representation clicked
               VisualizationItemPanel visPanel = (VisualizationItemPanel)selectedNode.getUserObject();
               ModalityItemPanel modalityPanel =
                  (ModalityItemPanel)( (DefaultMutableTreeNode)selectedNode.getParent() ).getUserObject();

               Application.viewManager.openVisualization( modalityPanel.modality, visPanel.visualization );

            } else if( selectedNode.getUserObject() instanceof ModalityItemPanel ) {
               if( collapseOnModality ) {
                  if( datasetTree.isCollapsed( tp ) ) {
                     datasetTree.expandPath( tp );
                  } else {
                     datasetTree.collapsePath( tp );
                  }
               }
            }
            // ignore other double clicks (double click on representation will
            // unfold that modality)
         }
      }
   }

   public void mouseClicked( MouseEvent e ) {
      if( e.getClickCount() == 2 && e.getSource() == datasetTree ) {
         // double click on the table
         handleActionOnSelectedTreePaths( datasetTree.getSelectionPaths(), false );
         e.consume();
      }
   }

   public void keyPressed( KeyEvent e ) {
   }

   public void keyReleased( KeyEvent e ) {
      if( e.getSource() == datasetTree && e.getKeyCode() == KeyEvent.VK_ENTER ) {
         handleActionOnSelectedTreePaths( datasetTree.getSelectionPaths(), true );
         e.consume();
      }

   }

   public void keyTyped( KeyEvent e ) {
   }

}
